<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/thor-toolkit" prefix="tt" %>
<!DOCTYPE html>
<html>
<head>
	<tt:env/>
    <title>对标管理平台</title>
    <meta charset="utf-8">
    <%@include file="../common-include.jspf"%>
    <script>
         $(function(){
        	 
        	var leftList=tui.ctrl.list("leftList");
        	var rightList=tui.ctrl.list("rightList");
        	
           	tui.ctrl.form("getPermissionGroupForm").on("receive",function(data){
           		leftList.data(data);
           	});
           	
        	tui.ctrl.form("getPointsForm").on("receive",function(data){
        		rightList.data(data);
           	});
        	
        	var lastArr=[];
        	var groupId;
        /**
        	function listPointsByGroup(item){
        		var fm=tui.ctrl.form();
        		fm.id("getPointsIdForm");
        		fm.action("${root}/action/sysListPointsId");
        		fm.immediateValue({"id":item.key});
        		fm.submit();
        		rightList.uncheckItems(lastArr);
        		fm.on("receive",function(data){
        			var arr=[];
        			for(var i=0;i<data.length;i++){
        				arr.push(data[i].points_id);
        			}
        			lastArr=arr;
        			rightList.doCheck(arr,true);
        		});
        		if(item.children){
        			for(var i=0;i<item.children.length;i++){
        				listPointsByGroup(item.children[i].key);
        			}
        			alert(lastArr);
        		}
        	}
        	*/
        	/*
        	var flag=false;
        	function listPointsByGroup(item){
        		var arr=[];
        		var fm=tui.ctrl.form();
        		fm.id("getPointsIdForm");
        		fm.action("${root}/action/sysListPointsId");
        		fm.immediateValue({"id":item.key});
        		fm.submit();
        		fm.once("receive",function(data){
        			for(var i=0;i<data.length;i++){
        				arr.push(data[i].points_id);
        			}
        			//alert(arr);
        			lastArr=arr;
        			//rightList.doCheck(arr,true);
        			rightList.doCheck(unique(arr),true);
        		});
        		if(item.children){
        			for(var i=0;i<item.children.length;i++){
        				listPointsByGroup(item.children[i]);
        			}
        		}
        		
        		rightList.uncheckAllItems();
        		
        		
        	}
        	*/
        	var listPointsfm=tui.ctrl.form();
        	function listPointsByGroup(item){
        		var arr=[];
        		
        		listPointsfm.id("getPointsIdForm");
        		listPointsfm.action("${root}/action/sysListPointsId");
        		listPointsfm.immediateValue({"id":item.key});
        		listPointsfm.submit();
        		var data;
        		listPointsfm.once("receive",function(data){
        			data=data;
        			for(var i=0;i<data.length;i++){
        				arr.push(data[i].points_id);
        			}
        			//alert(arr);
        			//rightList.doCheck(arr,true);
        			rightList.doCheck(arr,true);
        			return data;
        		});
        		rightList.uncheckAllItems();
        		
        		
        	}
        	
        	function listSectionByGroup(item){
        		var arr=[];
        		var fm=tui.ctrl.form();
        		fm.id("getSectionIdForm");
        		fm.action("${root}/action/sysListSectionId");
        		fm.immediateValue({"id":item.key});
        		fm.submit();
        		fm.once("receive",function(data){
        			for(var i=0;i<data.length;i++){
        				arr.push(data[i].section_id);
        			}
        			//alert(arr);
        			//rightList.doCheck(arr,true);
        			sectionList.doCheck(arr,true);
        		});
        		sectionList.uncheckAllItems();
        		
        		
        	}
        	function unique(arr) {
        	    var result = [], hash = {};
        	    for (var i = 0, elem; (elem = arr[i]) != null; i++) {
        	        if (!hash[elem]) {
        	            result.push(elem);
        	            hash[elem] = true;
        	        }
        	    }
        	    return result;
        	//http://www.cnblogs.com/sosoft/
        	}
        	var groupItem;
        	leftList.on("rowclick",function(data){
        		var item=leftList.activeItem();
        		groupItem=item;
        		var id=leftList.activeItem().key;
        		$("#pointsNameSp1")[0].innerHTML=item.value;
        		$("#pointsNameSp2")[0].innerHTML=item.value;
        		groupId=id;
        		listPointsByGroup(item);
        		listSectionByGroup(item);
        	});
        	
        	
        	var sectionList=tui.ctrl.list("sectionList");
        	sectionList.on("rowcheck",function(data){
        		var fm=tui.ctrl.form();
           		var item=sectionList.activeItem();
           		if(!groupId){
           			tui.infobox("请选择组","请选择组");
           			return;
           		}
           		if(item.checked){
           			//添加
           			tui.askbox("确定添加此板块权限?","确认权限",function(result){
           				if(result){
           					fm.id("addSectionTogroup");
                   			fm.action("${root}/action/sysAddSectionToGroup");
                   			var obj="{"+groupId;
                 			if(groupItem.__parent){
             						obj=obj+","+groupItem.__parent.key +"}";
             						fm.immediateValue({"groupId":obj,"sectionId":item.key});
               					
               				}else{
               					obj=obj+"}";
               					fm.immediateValue({"groupId":obj,"sectionId":item.key});
               				}
                 			fm.submit();
                 			
           					fm.on("success",function(){
           						
           					});
           				}else{
           					item.checked=false;
           					listSectionByGroup(groupItem);
           				}
           			});
           		}else{
           			//删除
           			fm.id("deleteSectionFromgroup");
                	fm.action("${root}/action/sysDeleteSectionFromGroup");
           			if(groupItem.children){
	           			tui.askbox("此操作将会删除此组及其子组的此板块权限,<br>确认删除?","确认权限",function(result){
	           				if(result){
	           					var obj="{"+groupId+",";
           						for(var i=0;i<groupItem.children.length;i++){
           							if(i==groupItem.children.length-1){
           								obj=obj+groupItem.children[i].key;
           							}else{
           								obj=obj+groupItem.children[i].key+",";
           							}
           						}
           						obj=obj+"}";
	                 			fm.immediateValue({"groupId":obj,"sectionId":item.key});
	                   			fm.submit();
	           					fm.on("success",function(){
	           						listSectionByGroup(groupItem);
	           					});
	        				}else{
	        					item.checked=false;
	        					listSectionByGroup(groupItem);
	        				}
	           			});
           			}else{
           				tui.askbox("确定删除此板块权限?","确认权限",function(result){
	           				if(result){
	           					var obj="{"+groupId+"}";
	                 			fm.immediateValue({"groupId":obj,"sectionId":item.key});
	                   			fm.submit();
	           					fm.on("success",function(){
	           					});
	        				}else{
	        					item.checked=false;
	        					listSectionByGroup(groupItem);
	        				}
	           			});
           			}
           		}
        	});
        	
        	rightList.on("rowcheck",function(data){
           		var fm=tui.ctrl.form();
           		var item=rightList.activeItem();
           		if(!groupId){
           			tui.infobox("请选择组","请选择组");
           			return;
           		}
           		if(item.checked){
           			//添加
           			tui.askbox("确定添加此功能权限?","确认权限",function(result){
           				var groupArr=[];
               			var pointArr=[];
           				if(result){
           					fm.id("addPointsTogroup");
                   			fm.action("${root}/action/sysAddPointsToGroup");
                   			groupArr.push(groupId);
                   			pointArr.push(item.key);
                   			if(item.__parent&& ((item.__parent.checked)!=true)){
                   				pointArr.push(item.__parent.key);
                   			}
           					if(groupItem.__parent){
               					groupArr.push(groupItem.__parent.key);
               				}
           					var obj11="{"
           					for(var i=0;i<groupArr.length;i++){
           						var obj11=obj11+groupArr[i];
           						if(i!==groupArr.length-1)
           							obj11=obj11+",";
           					}
           					var obj1=obj11+"}"	
           					var obj22="{"
               					for(var i=0;i<pointArr.length;i++){
               						var obj22=obj22+pointArr[i];
               						if(i!==pointArr.length-1)
               							obj22=obj22+",";
               					}
               					var obj2=obj22+"}"	
           					
           					fm.immediateValue({"groupId":obj1,"pointsId":obj2});
                   			fm.submit();
           					fm.on("success",function(){
	           					listPointsByGroup(groupItem);
           					});
           				}else{
           					item.checked=false;
           					listPointsByGroup(groupItem);
           				}
           			});
           		}else{
           			//删除
           			fm.id("deletePointsTogroup");
                 	fm.action("${root}/action/sysDeletePointsFromGroup");
                 	// 删除子组某权限的时候,删除父子的权限
           			if(groupItem.children){
	           			tui.askbox("此操作将会删除此组及其子组的此功能权限,<br>确认删除?","确认权限",function(result){
	           				if(result){
	           					var obj1="{"+groupId+",";
	           					for(var i=0;i<groupItem.children.length;i++){
           							if(i==groupItem.children.length-1){
           								obj1=obj1+groupItem.children[i].key;
           							}else{
           								obj1=obj1+groupItem.children[i].key+",";
           							}
           						}
	           					obj1=obj1+"}";
	           					
	           					var obj2="{"+item.key;
	           					if(item.children){
	           						obj2=obj2+","+item.children[0].key;
	           					}
	           					obj2=obj2+"}";
           						
           						fm.immediateValue({"id":obj2,"groupId":obj1});
           						fm.submit();
           						fm.on("success",function(){
		           					listPointsByGroup(groupItem);
	           					});
	           				}else{
	           					item.checked=false;
	           					listPointsByGroup(groupItem);
	           				}
	           			});
           			}else{
	           			tui.askbox("确定删除此功能权限?","确认权限",function(result){
	           				if(result){
	           					fm.id("deletePointsTogroup");
	                   			fm.action("${root}/action/sysDeletePointsFromGroup");
	                   			//fm.immediateValue({"id":item.key});
	                   			var obj1="{"+groupId+"}";
	                   			
	                   			var obj2="{"+item.key;
	           					if(item.children){
	           						obj2=obj2+","+item.children[0].key;
	           					}
	           					obj2=obj2+"}";
           						fm.immediateValue({"id":obj2,"groupId":obj1});
           						fm.submit();
	           					fm.on("success",function(){
		           					listPointsByGroup(groupItem);
	           					});
	        				}else{
	        					item.checked=false;
	           					listPointsByGroup(groupItem);
	        				}
	           			});
           			}
           		}
           	});
        	
        	var getReportSectionFm=tui.ctrl.form("getReportSection");
        	getReportSectionFm.submit();
        	getReportSectionFm.on("receive",function(data){
   				for(var i=0;i<data.length;i++){
   					data[i]={checked:false,key:data[i].key,value:data[i].value};
   				}
   				sectionList.data(data);
   			});
   			
         });
    </script>
</head>
<body style="margin:0px;background-color:#E7E9ED;">
    <%@include file="../common-header.jspf"%>
    <div style="width:95%;margin:0 auto;min-width:1200px ;text-align:center;padding:10px 0 0 0">
	    <div style="margin:0 auto;display:inline-block;width:30%;margin-right:30px;vertical-align:top">
	    	<h3>用户组列表</h3>
	    	<div id="leftList" class="tui-list"  data-key-is-link="true" 
	            style="height:545px;background-color:#fafafa;
	            box-shadow: inset 1px 1px 3px rgba(100,100,100,0.1);width:100%" ></div>
	    </div>
	    <div style="margin:0 auto;display:inline-block;width:30%;">
	    	<div >
		    	<h3><span id="pointsNameSp1"></span>板块列表</h3>
		    	<div id="sectionList" class="tui-list"  data-key-is-link="true" 
		            style="height:245px;background-color:#fafafa;
		            box-shadow: inset 1px 1px 3px rgba(100,100,100,0.1);width:100%"></div>
		    </div>
		    <div >
		    	<h3><span id="pointsNameSp2"></span>功能列表</h3>
		    	<div id="rightList" class="tui-list"  data-key-is-link="true" 
		            style="height:245px;background-color:#fafafa;
		            box-shadow: inset 1px 1px 3px rgba(100,100,100,0.1);width:100%"></div>
		    </div>
	    </div>
	    
    </div>
</body>
<span id="getPermissionGroupForm" class="tui-form" data-method="get" data-auto-submit="true" data-action="${root}/action/sysListPermissionGroup"></span>
<span id="getPointsForm" class="tui-form" data-method="get" data-auto-submit="true" data-action="${root}/action/sysListInitPoints"></span>
<span id="getReportSection" class="tui-form" data-method="get" data-action="${root}/action/sysListSection"></span>
</html>
